# Load required packages
library(cjoint)

# Load the data
load("survey_data_nominees_final.RData")

############################################
# Table B.1: Survey descriptive statistics #
############################################
# Gender
round(prop.table(table(survey.dataset$GenderIdentity)),3)

# Race - create categories of white, black, hispanic/latino, asian, multiple races, other
round(length(which(survey.dataset$Race %in% c("White")))/length(survey.dataset$Race),3)
round(length(which(survey.dataset$Race %in% c("Black or African American")))/length(survey.dataset$Race),3)
round(length(which(survey.dataset$Race %in% c("Hispanic or Latino","White,Hispanic or Latino")))/length(survey.dataset$Race),3)
round(length(which(survey.dataset$Race %in% c("Asian","Native Hawaiian or Pacific Islander","Asian,Native Hawaiian or Pacific Islander")))/length(survey.dataset$Race),3)
round(length(which(survey.dataset$Race %in% c("American Indian or Alaska Native")))/length(survey.dataset$Race),3)
round(length(which(survey.dataset$Race %in% c("Black or African American,American Indian or Alaska Native","Black or African American,Hispanic or Latino",
                                                "Black or African American,Hispanic or Latino,American Indian or Alaska Native","Hispanic or Latino,Asian",
                                                "White,American Indian or Alaska Native","White,Asian","White,Asian,American Indian or Alaska Native,Native Hawaiian or Pacific Islander",
                                                "White,Asian,Native Hawaiian or Pacific Islander","White,Black or African American","White,Black or African American,American Indian or Alaska Native",
                                                "White,Black or African American,Asian,American Indian or Alaska Native","White,Black or African American,Hispanic or Latino",
                                                "White,Hispanic or Latino,American Indian or Alaska Native","White,Hispanic or Latino,Asian",
                                                "White,Other","Black or African American,Asian","Hispanic or Latino,American Indian or Alaska Native","White,Hispanic or Latino,Other",
                                                "White,Native Hawaiian or Pacific Islander")))/length(survey.dataset$Race),3)
round(length(which(survey.dataset$Race %in% c("Other")))/length(survey.dataset$Race),3)

# Partisanship, coding leaners as partisans
round(prop.table(table(survey.dataset$Party)),3)

# Education
round(prop.table(table(survey.dataset$Education)),3)

# Income
round(prop.table(table(survey.dataset$Income)),3)

# Ideology
round(prop.table(table(survey.dataset$Ideology_3)),3)

# Sexuality
round(prop.table(table(survey.dataset$Sexuality)),3)

#####################################################
# Figure D.1: Full support results, all respondents #
#####################################################
# Subsetting to respondents who answered the support question
support.dataset <- survey.dataset[which(!is.na(survey.dataset$support_nominee)),]

# Default baselines
baselines_list <- list()

# Estimate the AMCEs
results.support <- amce(support_nominee ~ judge_age + judge_race + judge_law_school + judge_job + judge_politics
                        + judge_transgender + judge_sexuality + judge_rhetoric, data=support.dataset, 
                        baselines = baselines_list, cluster=T, respondent.id = "ResponseId")
summary(results.support)

# Setting attribute names to display when plotting
attribute.names.full <- c("Age","Job","Law School","Political Views","Race","Rhetoric","Sexuality","Gender")

# Setting specific attributes that will be displayed in the plot output
levels.test.full<-list()
levels.test.full[["judge_age"]] <- c("45","55","65")
levels.test.full[["judge_race"]] <- c("Black","Asian","Hispanic")
levels.test.full[["judge_law_school"]] <- c("Elite Ivy","Public","Not top 100")
levels.test.full[["judge_job"]] <- c("Judge","Public defender","Professor","Defense attorney")
levels.test.full[["judge_politics"]] <- c("Very liberal","Liberal","Somewhat liberal","Moderate")
levels.test.full[["judge_transgender"]] <-c("Cisgender","Transgender")
levels.test.full[["judge_sexuality"]] <-c("Straight","Gay or lesbian")
levels.test.full[["judge_rhetoric"]] <-c("No statement from Biden","Statement from Biden")

# Plotting
pdf(file="figures/figure-d1-final.pdf", width=8,height=8)
plot(results.support, attribute.names = attribute.names.full,
     label.baseline=F, level.names=levels.test.full, main = "", xlab="Change in Support", xlim=c(-.25,.25),
     color="black", ylim=c(-1,1))
dev.off()

##############################################
# Figure D.2: Full support results, by party #
##############################################
# Estimate the AMCEs, results by respondent partisanship
results.support.by.party <- amce(support_nominee ~ judge_age*Party + judge_race*Party 
                                 + judge_law_school*Party + judge_job*Party 
                                 + judge_politics*Party + judge_transgender*Party 
                                 + judge_sexuality*Party+ judge_rhetoric*Party, 
                                 data=support.dataset, respondent.varying="Party", baselines = baselines_list, cluster=T, 
                                 respondent.id = "ResponseId")
summary(results.support.by.party)

# Plotting
pdf(file="figures/figure-d2-final.pdf", width=8,height=8)
plot(results.support.by.party, plot.display="interaction", attribute.names = attribute.names.full,
     label.baseline=F, level.names=levels.test.full, main = "", xlab="Change in Support", xlim=c(-.4,.4),
     color="black")
dev.off()

############################################
# Figure D.3: Full results, double penalty #
############################################
# Creating a variable for both/one/neither transgender and gay treatment
support.dataset$judge_combined_transgender_gay <- ifelse(support.dataset$judge_transgender == "Transgender" & support.dataset$judge_sexuality == "Gay or lesbian", "Transgender and gay",
                                                         ifelse((support.dataset$judge_transgender == "Transgender" & support.dataset$judge_sexuality == "Straight") | 
                                                                  (support.dataset$judge_transgender == "Not transgender" & support.dataset$judge_sexuality == "Gay or lesbian"), "Just transgender or just gay","Cisgender and straight"))
support.dataset$judge_combined_transgender_gay <- as.factor(support.dataset$judge_combined_transgender_gay)

# Setting baselines
baselines_list <- list()
# Making just transgender or just gay the baseline
baselines_list$judge_combined_transgender_gay <- "Just transgender or just gay"

# Estimate the AMCEs for gay/transgender double penalty 
results.both.trans.gay.support.pooled <- amce(support_nominee ~ judge_age + judge_race 
                                              + judge_law_school + judge_job 
                                              + judge_politics + judge_combined_transgender_gay
                                              + judge_rhetoric,
                                              data=support.dataset, baselines = baselines_list, cluster=T, 
                                              respondent.id = "ResponseId")
summary(results.both.trans.gay.support.pooled)

# Setting attribute names to display when plotting
attribute.names.full <- c("Age","Gender and Sexuality","Job","Law School","Political Views","Race","Rhetoric")

# Change the specific attributes that will be displayed in the plot output
levels.test.full<-list()
levels.test.full[["judge_age"]] <- c("45","55","65")
levels.test.full[["judge_race"]] <- c("Black","Asian","Hispanic")
levels.test.full[["judge_law_school"]] <- c("Elite Ivy","Public","Not top 100")
levels.test.full[["judge_job"]] <- c("Judge","Public defender","Professor","Defense attorney")
levels.test.full[["judge_politics"]] <- c("Very liberal","Liberal","Somewhat liberal","Moderate")
levels.test.full[["judge_combined_transgender_gay"]] <- c("Just transgender or just gay","Cisgender and straight","Transgender and gay")
levels.test.full[["judge_rhetoric"]] <-c("No statement from Biden","Statement from Biden")

# Plotting
pdf(file="figures/figure-d3-final.pdf", width=8,height=8)
plot(results.both.trans.gay.support.pooled, attribute.names = attribute.names.full,
     label.baseline=F, level.names=levels.test.full, main = "", xlab="Change in Support", xlim=c(-.25,.25),
     color="black", ylim=c(-1,1))
dev.off()

###############################################
# Figure D.4: Full results, men nominees only #
###############################################
# Subsetting to just men or transgender men nominees
support.dataset.just.men <- support.dataset[which(support.dataset$judge_gender %in% c("Man","Transgender man")),]
# Revised gender variable
support.dataset.just.men$judge_gender_revised <- as.factor(ifelse(support.dataset.just.men$judge_gender == "Man","Man","Transgender man"))

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs for all respondents, just men or transgender men nominees
results.support.just.men <- amce(support_nominee ~ judge_age + judge_race + judge_law_school + judge_job + judge_politics
                                 + judge_gender_revised + judge_sexuality + judge_rhetoric, data=support.dataset.just.men, 
                                 baselines = baselines_list, cluster=T, respondent.id = "ResponseId")
summary(results.support.just.men)

# Setting attribute names to display when plotting
attribute.names.full <- c("Age","Gender","Job","Law School","Political Views","Race","Rhetoric","Sexuality")

# Change the specific attributes that will be displayed in the plot output
levels.test.full<-list()
levels.test.full[["judge_age"]] <- c("45","55","65")
levels.test.full[["judge_race"]] <- c("Black","Asian","Hispanic")
levels.test.full[["judge_law_school"]] <- c("Elite Ivy","Public","Not top 100")
levels.test.full[["judge_job"]] <- c("Judge","Public defender","Professor","Defense attorney")
levels.test.full[["judge_politics"]] <- c("Very liberal","Liberal","Somewhat liberal","Moderate")
# Just two gender treatments
levels.test.full[["judge_gender_revised"]] <-c("Man","Transgender man")
levels.test.full[["judge_sexuality"]] <-c("Straight","Gay")
levels.test.full[["judge_rhetoric"]] <-c("No statement from Biden","Statement from Biden")

# Plotting
pdf(file="figures/figure-d4-final.pdf", width=8,height=8)
plot(results.support.just.men, attribute.names = attribute.names.full,
     label.baseline=F, level.names=levels.test.full, main = "", xlab="Change in Support", xlim=c(-.25,.25),
     color="black", ylim=c(-1,1))
dev.off()

#################################################
# Figure D.5: Full results, women nominees only #
#################################################
# Subsetting to just women or transgender women nominees
support.dataset.just.women <- support.dataset[which(support.dataset$judge_gender %in% c("Woman","Transgender woman")),]
# Revised gender variable
support.dataset.just.women$judge_gender_revised <- as.factor(ifelse(support.dataset.just.women$judge_gender == "Woman","Woman","Transgender woman"))
support.dataset.just.women$judge_gender_revised <- relevel(support.dataset.just.women$judge_gender_revised, ref="Woman")

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs for all respondents, just women or transgender women nominees
results.support.just.women <- amce(support_nominee ~ judge_age + judge_race + judge_law_school + judge_job + judge_politics
                                   + judge_gender_revised + judge_sexuality + judge_rhetoric, data=support.dataset.just.women, 
                                   baselines = baselines_list, cluster=T, respondent.id = "ResponseId")
summary(results.support.just.women)

# Setting attribute names to display when plotting
attribute.names.full <- c("Age","Gender","Job","Law School","Political Views","Race","Rhetoric","Sexuality")

# Change the specific attributes that will be displayed in the plot output
levels.test.full<-list()
levels.test.full[["judge_age"]] <- c("45","55","65")
levels.test.full[["judge_race"]] <- c("Black","Asian","Hispanic")
levels.test.full[["judge_law_school"]] <- c("Elite Ivy","Public","Not top 100")
levels.test.full[["judge_job"]] <- c("Judge","Public defender","Professor","Defense attorney")
levels.test.full[["judge_politics"]] <- c("Very liberal","Liberal","Somewhat liberal","Moderate")
# Just two gender treatments
levels.test.full[["judge_gender_revised"]] <-c("Woman","Transgender woman")
levels.test.full[["judge_sexuality"]] <-c("Straight","Lesbian")
levels.test.full[["judge_rhetoric"]] <-c("No statement from Biden","Statement from Biden")

# Plotting
pdf(file="figures/figure-d5-final.pdf", width=8,height=8)
plot(results.support.just.women, attribute.names = attribute.names.full,
     label.baseline=F, level.names=levels.test.full, main = "", xlab="Change in Support", xlim=c(-.25,.25),
     color="black", ylim=c(-1,1))
dev.off()

#############################################
# Figure E.1: Five-point measure of support #
#############################################
# Setting baselines
baselines_list <- list()

# Estimate the AMCEs by party, 5pt outcome variable
results.support.5pt.by.party <- amce(support_nominee_5pt ~ judge_age*Party + judge_race*Party 
                                     + judge_law_school*Party + judge_job*Party 
                                     + judge_politics*Party + judge_transgender*Party 
                                     + judge_sexuality*Party+ judge_rhetoric*Party,
                                     data=support.dataset, respondent.varying="Party", baselines = baselines_list, cluster=T, 
                                     respondent.id = "ResponseId")
summary(results.support.5pt.by.party)

### Plot of differences when moving from baseline ###
# Values come from above model
# All respondents first, then D, then R
coefs_from_baseline_sexuality <- c(-0.235090,
                                   -0.1328049,
                                   -0.2997566)
# All respondents first, then D, then R
coefs_from_baseline_gender <- c(-0.425239,
                                -0.3062171,
                                -0.5680411)

# Confidence intervals around the differences
sexuality_se <- c(0.056285,0.062896,0.072669)
gender_se <- c(0.058197,0.066483,0.072007)

sexuality_ci_low <- coefs_from_baseline_sexuality - (sexuality_se*1.96)
sexuality_ci_high <- coefs_from_baseline_sexuality + (sexuality_se*1.96)
sexuality_ci_low_90 <- coefs_from_baseline_sexuality - (sexuality_se*1.65)
sexuality_ci_high_90 <- coefs_from_baseline_sexuality + (sexuality_se*1.65)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e1-final.pdf", width=5.5,height=5.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_sexuality,c(0.5,0.35,0.2),ylim=c(0,2),xlim=c(-.8,.1), tck=0, cex.main=1.4, main="", cex.lab=0.95, cex.axis=1, 
     pch=c(16,17,15), cex=2, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
axis(1,at=seq(-.8,.1,by=.1), cex.axis=1.1, 
     labels=c("-.8","-.7","-.6","-.5","-.4","-.3","-.2","-.1","0",".1"), tck=0.01, las=1)
axis(2, at=c(0.35,1.35),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.2)
segments(sexuality_ci_low, c(0.5,0.35,0.2), sexuality_ci_high, c(0.5,0.35,0.2), lwd=1, col=c("black","blue","red"))
segments(sexuality_ci_low_90, c(0.5,0.35,0.2), sexuality_ci_high_90, c(0.5,0.35,0.2), lwd=3, col=c("black","blue","red"))

points(x=coefs_from_baseline_gender, y=c(1.5,1.35,1.2), pch=c(16,17,15), col=c("black","blue","red"), cex=2)
segments(gender_ci_low, c(1.5,1.35,1.2), gender_ci_high, c(1.5,1.35,1.2), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.5,1.35,1.2), gender_ci_high_90, c(1.5,1.35,1.2), lwd=3, col=c("black","blue","red"))
dev.off()

#################################################
# Figure E.2: Dropping middle response category #
#################################################
# Setting baselines
baselines_list <- list()

# Subsetting the dataset to drop middle responses
dataset.no.middle <- support.dataset[which(support.dataset$support_nominee_5pt != 3),]

# Estimate the AMCEs by party, no middle responses
results.support.by.party.no.middle <- amce(support_nominee ~ judge_age*Party + judge_race*Party 
                                           + judge_law_school*Party + judge_job*Party 
                                           + judge_politics*Party + judge_transgender*Party 
                                           + judge_sexuality*Party+ judge_rhetoric*Party,
                                           data=dataset.no.middle, respondent.varying="Party", baselines = baselines_list, cluster=T, 
                                           respondent.id = "ResponseId")
summary(results.support.by.party.no.middle)

### Plot of differences when moving from baseline ###
# Values come from above model
# All respondents first, then D, then R
coefs_from_baseline_sexuality <- c(-0.0949861,
                                   -0.0553356,
                                   -0.119871)
# All respondents first, then D, then R
coefs_from_baseline_gender <- c(-0.1615210,
                                -0.1005397,
                                -0.192893)

# Confidence intervals around the differences
sexuality_se <- c(0.021226,0.022599,0.027711)
gender_se <- c(0.021829,0.024011,0.027296)

sexuality_ci_low <- coefs_from_baseline_sexuality - (sexuality_se*1.96)
sexuality_ci_high <- coefs_from_baseline_sexuality + (sexuality_se*1.96)
sexuality_ci_low_90 <- coefs_from_baseline_sexuality - (sexuality_se*1.65)
sexuality_ci_high_90 <- coefs_from_baseline_sexuality + (sexuality_se*1.65)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e2-final.pdf", width=5.5,height=5.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_sexuality,c(0.5,0.35,0.2),ylim=c(0,2),xlim=c(-.3,.1), tck=0, cex.main=1.4, main="", cex.lab=0.95, cex.axis=1, 
     pch=c(16,17,15), cex=2, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
axis(1,at=seq(-.3,.1,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1"), tck=0.01, las=1)
axis(2, at=c(0.35,1.35),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.2)
segments(sexuality_ci_low, c(0.5,0.35,0.2), sexuality_ci_high, c(0.5,0.35,0.2), lwd=1, col=c("black","blue","red"))
segments(sexuality_ci_low_90, c(0.5,0.35,0.2), sexuality_ci_high_90, c(0.5,0.35,0.2), lwd=3, col=c("black","blue","red"))

points(x=coefs_from_baseline_gender, y=c(1.5,1.35,1.2), pch=c(16,17,15), col=c("black","blue","red"), cex=2)
segments(gender_ci_low, c(1.5,1.35,1.2), gender_ci_high, c(1.5,1.35,1.2), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.5,1.35,1.2), gender_ci_high_90, c(1.5,1.35,1.2), lwd=3, col=c("black","blue","red"))
dev.off()

##############################################
# Figure E.3: Coding leaners as independents #
##############################################
# Setting baselines
baselines_list <- list()

# Estimate the AMCEs by party, leaners coded as independents
results.support.by.true.party <- amce(support_nominee ~ judge_age*True_Party + judge_race*True_Party 
                                      + judge_law_school*True_Party + judge_job*True_Party 
                                      + judge_politics*True_Party + judge_transgender*True_Party 
                                      + judge_sexuality*True_Party+ judge_rhetoric*True_Party,
                                      data=support.dataset, respondent.varying="True_Party", baselines = baselines_list, cluster=T, 
                                      respondent.id = "ResponseId")
summary(results.support.by.true.party)

### Plot of differences when moving from baseline ###
# All respondents first (same as main result above), then D, then R
coefs_from_baseline_sexuality <- c(-0.08096516,
                                   -0.0419328,
                                   -0.1083518)
# All respondents first (same as main result above), then D, then R
coefs_from_baseline_gender <- c(-0.13838154,
                                -0.1098852,
                                -0.1452258)

# Confidence intervals around the differences
sexuality_se <- c(0.019370,0.026478,0.025081)
gender_se <- c(0.020081,0.027965,0.024365)

sexuality_ci_low <- coefs_from_baseline_sexuality - (sexuality_se*1.96)
sexuality_ci_high <- coefs_from_baseline_sexuality + (sexuality_se*1.96)
sexuality_ci_low_90 <- coefs_from_baseline_sexuality - (sexuality_se*1.65)
sexuality_ci_high_90 <- coefs_from_baseline_sexuality + (sexuality_se*1.65)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e3-final.pdf", width=6.5,height=6.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_sexuality,c(0.5,0.35,0.2),ylim=c(0,2),xlim=c(-.3,.2), tck=0, cex.main=1.4, main="", cex.lab=1.2, cex.axis=1, 
     pch=c(16,17,15), cex=2.1, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
#lines(x=group, y=independents+jitter)
axis(1,at=seq(-.3,.2,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1",".2"), tck=0.01, las=1)
axis(2, at=c(0.35,1.35),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.4)
segments(sexuality_ci_low, c(0.5,0.35,0.2), sexuality_ci_high, c(0.5,0.35,0.2), lwd=1, col=c("black","blue","red"))
segments(sexuality_ci_low_90, c(0.5,0.35,0.2), sexuality_ci_high_90, c(0.5,0.35,0.2), lwd=3, col=c("black","blue","red"))

points(x=coefs_from_baseline_gender, y=c(1.5,1.35,1.2), pch=c(16,17,15), col=c("black","blue","red"), cex=2.1)
segments(gender_ci_low, c(1.5,1.35,1.2), gender_ci_high, c(1.5,1.35,1.2), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.5,1.35,1.2), gender_ci_high_90, c(1.5,1.35,1.2), lwd=3, col=c("black","blue","red"))
dev.off()

###################################
# Figure E.4: Results by ideology #
###################################
# Setting baselines
baselines_list <- list()

# Removing respondent with NA for ideology
ideology.support.dataset <- support.dataset[which(!is.na(support.dataset$Ideology_3)),]

# Estimate the AMCEs by ideology
results.support.by.ideology <- amce(support_nominee ~ judge_age*Ideology_3 + judge_race*Ideology_3 
                                    + judge_law_school*Ideology_3 + judge_job*Ideology_3 
                                    + judge_politics*Ideology_3 + judge_transgender*Ideology_3 
                                    + judge_sexuality*Ideology_3+ judge_rhetoric*Ideology_3,
                                    data=ideology.support.dataset, respondent.varying="Ideology_3", baselines = baselines_list, cluster=T, 
                                    respondent.id = "ResponseId")
summary(results.support.by.ideology)

### Plot of differences when moving from baseline ###
# All respondents first, then liberals, then conservatives
coefs_from_baseline_sexuality <- c(-0.08187999,
                                   -0.0377034,
                                   -0.0906719)
# All respondents first, then liberals, then conservatives
coefs_from_baseline_gender <- c(-0.13857730,
                                -0.1133352,
                                -0.1304906)

# Confidence intervals around the differences
sexuality_se <- c(0.019365,0.023965,0.024330)
gender_se <- c(0.020097,0.025846,0.024813)

sexuality_ci_low <- coefs_from_baseline_sexuality - (sexuality_se*1.96)
sexuality_ci_high <- coefs_from_baseline_sexuality + (sexuality_se*1.96)
sexuality_ci_low_90 <- coefs_from_baseline_sexuality - (sexuality_se*1.65)
sexuality_ci_high_90 <- coefs_from_baseline_sexuality + (sexuality_se*1.65)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e4-final.pdf", width=6.5,height=6.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_sexuality,c(0.5,0.35,0.2),ylim=c(0,2),xlim=c(-.3,.2), tck=0, cex.main=1.4, main="", cex.lab=1.2, cex.axis=1, 
     pch=c(16,17,15), cex=2.1, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
#lines(x=group, y=independents+jitter)
axis(1,at=seq(-.3,.2,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1",".2"), tck=0.01, las=1)
axis(2, at=c(0.35,1.35),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Liberals","Conservatives"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.4)
segments(sexuality_ci_low, c(0.5,0.35,0.2), sexuality_ci_high, c(0.5,0.35,0.2), lwd=1, col=c("black","blue","red"))
segments(sexuality_ci_low_90, c(0.5,0.35,0.2), sexuality_ci_high_90, c(0.5,0.35,0.2), lwd=3, col=c("black","blue","red"))

points(x=coefs_from_baseline_gender, y=c(1.5,1.35,1.2), pch=c(16,17,15), col=c("black","blue","red"), cex=2.1)
segments(gender_ci_low, c(1.5,1.35,1.2), gender_ci_high, c(1.5,1.35,1.2), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.5,1.35,1.2), gender_ci_high_90, c(1.5,1.35,1.2), lwd=3, col=c("black","blue","red"))
dev.off()

########################################################
# Figure E.5: Results with respondent-level covariates #
########################################################
# Setting baselines
baselines_list <- list()

# Removing NA respondents for respondent-level covariates
dataset.covariates.full <- support.dataset[complete.cases(support.dataset[c("white","income_over_75000","college_degree","man")]),]

# Estimate the AMCEs by party, model includes respondent-level covariates
results.support.by.party.respondent.characteristics <- amce(support_nominee ~ judge_age*Party + judge_race*Party 
                                                            + judge_law_school*Party + judge_job*Party 
                                                            + judge_politics*Party + judge_transgender*Party 
                                                            + judge_sexuality*Party+ judge_rhetoric*Party + white*Party + income_over_75000*Party + college_degree*Party + man*Party,
                                                            data=dataset.covariates.full, respondent.varying=c("Party"), baselines = baselines_list, cluster=T, 
                                                            respondent.id = "ResponseId")
summary(results.support.by.party.respondent.characteristics)

### Plot of differences when moving from baseline ###
# Values come from above model
# All respondents first, then D, then R
coefs_from_baseline_sexuality <- c(-0.0835308,
                                   -0.0539168,
                                   -0.1045897)
# All respondents first, then D, then R
coefs_from_baseline_gender <- c(-0.1339417,
                                -0.1285326,
                                -0.1485659)

# Confidence intervals around the differences
sexuality_se <- c(0.019413,0.024760,0.024459)
gender_se <- c(0.020028,0.025766,0.023482)

sexuality_ci_low <- coefs_from_baseline_sexuality - (sexuality_se*1.96)
sexuality_ci_high <- coefs_from_baseline_sexuality + (sexuality_se*1.96)
sexuality_ci_low_90 <- coefs_from_baseline_sexuality - (sexuality_se*1.65)
sexuality_ci_high_90 <- coefs_from_baseline_sexuality + (sexuality_se*1.65)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e5-final.pdf", width=5.5,height=5.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_sexuality,c(0.5,0.35,0.2),ylim=c(0,2),xlim=c(-.3,.1), tck=0, cex.main=1.4, main="", cex.lab=0.95, cex.axis=1, 
     pch=c(16,17,15), cex=2, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
axis(1,at=seq(-.3,.1,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1"), tck=0.01, las=1)
axis(2, at=c(0.35,1.35),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.2)
segments(sexuality_ci_low, c(0.5,0.35,0.2), sexuality_ci_high, c(0.5,0.35,0.2), lwd=1, col=c("black","blue","red"))
segments(sexuality_ci_low_90, c(0.5,0.35,0.2), sexuality_ci_high_90, c(0.5,0.35,0.2), lwd=3, col=c("black","blue","red"))

points(x=coefs_from_baseline_gender, y=c(1.5,1.35,1.2), pch=c(16,17,15), col=c("black","blue","red"), cex=2)
segments(gender_ci_low, c(1.5,1.35,1.2), gender_ci_high, c(1.5,1.35,1.2), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.5,1.35,1.2), gender_ci_high_90, c(1.5,1.35,1.2), lwd=3, col=c("black","blue","red"))
dev.off()

##########################################
# Figure E.6: First nominee profile only #
##########################################
# Subsetting the dataset to first profile evaluations only
dataset.first.profile <- support.dataset[which(support.dataset$profile_evaluation == "first"),]

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs by party, first profile only
results.support.by.party.first <- amce(support_nominee ~ judge_age*Party + judge_race*Party 
                                       + judge_law_school*Party + judge_job*Party 
                                       + judge_politics*Party + judge_transgender*Party 
                                       + judge_sexuality*Party+ judge_rhetoric*Party,
                                       data=dataset.first.profile, respondent.varying="Party", baselines = baselines_list, cluster=F)
summary(results.support.by.party.first)

### Plot of differences when moving from baseline ###
# All respondents first, then D, then R
coefs_from_baseline_sexuality <- c(-0.0867380,
                                   -0.0692979,
                                   -0.0894674)
# All respondents first, then D, then R
coefs_from_baseline_gender <- c(-0.1254900,
                                -0.1498310,
                                -0.1023336)

# Confidence intervals around the differences
sexuality_se <- c(0.027844,0.035544,0.032743)
gender_se <- c(0.027790,0.034996,0.032579)

sexuality_ci_low <- coefs_from_baseline_sexuality - (sexuality_se*1.96)
sexuality_ci_high <- coefs_from_baseline_sexuality + (sexuality_se*1.96)
sexuality_ci_low_90 <- coefs_from_baseline_sexuality - (sexuality_se*1.65)
sexuality_ci_high_90 <- coefs_from_baseline_sexuality + (sexuality_se*1.65)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e6-final.pdf", width=6.5,height=6.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_sexuality,c(0.5,0.35,0.2),ylim=c(0,2),xlim=c(-.3,.2), tck=0, cex.main=1.4, main="", cex.lab=1.2, cex.axis=1, 
     pch=c(16,17,15), cex=2.1, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
#lines(x=group, y=independents+jitter)
axis(1,at=seq(-.3,.2,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1",".2"), tck=0.01, las=1)
axis(2, at=c(0.35,1.35),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.4)
segments(sexuality_ci_low, c(0.5,0.35,0.2), sexuality_ci_high, c(0.5,0.35,0.2), lwd=1, col=c("black","blue","red"))
segments(sexuality_ci_low_90, c(0.5,0.35,0.2), sexuality_ci_high_90, c(0.5,0.35,0.2), lwd=3, col=c("black","blue","red"))

points(x=coefs_from_baseline_gender, y=c(1.5,1.35,1.2), pch=c(16,17,15), col=c("black","blue","red"), cex=2.1)
segments(gender_ci_low, c(1.5,1.35,1.2), gender_ci_high, c(1.5,1.35,1.2), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.5,1.35,1.2), gender_ci_high_90, c(1.5,1.35,1.2), lwd=3, col=c("black","blue","red"))
dev.off()

################################################
# Figure E.7: Dropping inattentive respondents #
################################################
# Subsetting the dataset to those who passed the attention check
dataset.attention <- support.dataset[which(support.dataset$passed_attention == 1),]

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs by party, respondents who passed attention check
results.support.by.party.attention <- amce(support_nominee ~ judge_age*Party + judge_race*Party 
                                           + judge_law_school*Party + judge_job*Party 
                                           + judge_politics*Party + judge_transgender*Party 
                                           + judge_sexuality*Party+ judge_rhetoric*Party,
                                           data=dataset.attention, respondent.varying="Party", baselines = baselines_list, cluster=T, 
                                           respondent.id = "ResponseId")
summary(results.support.by.party.attention)

### Plot of differences when moving from baseline ###
# All respondents first, then D, then R
coefs_from_baseline_sexuality <- c(-0.07829508,
                                   -0.0557017,
                                   -0.0931730)
# All respondents first, then D, then R
coefs_from_baseline_gender <- c(-0.13906758,
                                -0.1237248,
                                -0.1595280)

# Confidence intervals around the differences
sexuality_se <- c(0.019610,0.025290,0.024334)
gender_se <- c(0.020318,0.026064,0.023489)

sexuality_ci_low <- coefs_from_baseline_sexuality - (sexuality_se*1.96)
sexuality_ci_high <- coefs_from_baseline_sexuality + (sexuality_se*1.96)
sexuality_ci_low_90 <- coefs_from_baseline_sexuality - (sexuality_se*1.65)
sexuality_ci_high_90 <- coefs_from_baseline_sexuality + (sexuality_se*1.65)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e7-final.pdf", width=6.5,height=6.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_sexuality,c(0.5,0.35,0.2),ylim=c(0,2),xlim=c(-.3,.2), tck=0, cex.main=1.4, main="", cex.lab=1.2, cex.axis=1, 
     pch=c(16,17,15), cex=2.1, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
#lines(x=group, y=independents+jitter)
axis(1,at=seq(-.3,.2,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1",".2"), tck=0.01, las=1)
axis(2, at=c(0.35,1.35),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.4)
segments(sexuality_ci_low, c(0.5,0.35,0.2), sexuality_ci_high, c(0.5,0.35,0.2), lwd=1, col=c("black","blue","red"))
segments(sexuality_ci_low_90, c(0.5,0.35,0.2), sexuality_ci_high_90, c(0.5,0.35,0.2), lwd=3, col=c("black","blue","red"))

points(x=coefs_from_baseline_gender, y=c(1.5,1.35,1.2), pch=c(16,17,15), col=c("black","blue","red"), cex=2.1)
segments(gender_ci_low, c(1.5,1.35,1.2), gender_ci_high, c(1.5,1.35,1.2), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.5,1.35,1.2), gender_ci_high_90, c(1.5,1.35,1.2), lwd=3, col=c("black","blue","red"))
dev.off()

##################################################
# Figure E.8: Results by respondent LGBTQ traits #
##################################################
# Subsetting to respondents who indicate that they are not straight and/or not cisgender
dataset.lgbtq.respondents <- support.dataset[which(support.dataset$gay_bisexual_other == 1 | support.dataset$transgender == 1),]

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs, LGBTQ respondents
results.support.lgbtq.respondents <- amce(support_nominee ~ judge_age + judge_race + judge_law_school + judge_job + judge_politics
                                          + judge_transgender + judge_sexuality + judge_rhetoric, data=dataset.lgbtq.respondents, 
                                          baselines = baselines_list, cluster=T, respondent.id = "ResponseId")
summary(results.support.lgbtq.respondents)

# Subsetting to respondents who indicate that they are straight and cisgender
dataset.straight.cisgender.respondents <- support.dataset[which(support.dataset$gay_bisexual_other == 0 & support.dataset$transgender == 0),]

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs, straight and cisgender respondents
results.support.straight.cisgender.respondents <- amce(support_nominee ~ judge_age + judge_race + judge_law_school + judge_job + judge_politics
                                                       + judge_transgender + judge_sexuality + judge_rhetoric, data=dataset.straight.cisgender.respondents, 
                                                       baselines = baselines_list, cluster=T, respondent.id = "ResponseId")
summary(results.support.straight.cisgender.respondents)

### Plot of differences when moving from baseline ###
# Values come from above models, LGBTQ respondents first than straight and cisgender respondents
coef_from_baseline_gay_identity <- c(-0.010557,-0.08936700)
# Transgender, man first than woman
coef_from_baseline_trans_identity <- c(-0.076809,-0.14464708)

# Confidence intervals around the differences
gay_identity_se <- c(0.054008,0.020615)
trans_identity_se <- c(0.051667,0.021093)

gay_identity_ci_low_pooled <- coef_from_baseline_gay_identity - (gay_identity_se*1.96)
gay_identity_ci_high_pooled <- coef_from_baseline_gay_identity + (gay_identity_se*1.96)
gay_identity_ci_low_pooled_90 <- coef_from_baseline_gay_identity - (gay_identity_se*1.65)
gay_identity_ci_high_pooled_90 <- coef_from_baseline_gay_identity + (gay_identity_se*1.65)

trans_identity_ci_low_pooled <- coef_from_baseline_trans_identity - (trans_identity_se*1.96)
trans_identity_ci_high_pooled <- coef_from_baseline_trans_identity + (trans_identity_se*1.96)
trans_identity_ci_low_pooled_90 <- coef_from_baseline_trans_identity - (trans_identity_se*1.65)
trans_identity_ci_high_pooled_90 <- coef_from_baseline_trans_identity + (trans_identity_se*1.65)

# Plotting
pdf(file="figures/figure-e8-final.pdf", width=5.5,height=5.5)
par(mar=c(4.5,6.5,1,1), mgp=c(2,.25,0))
plot(coef_from_baseline_gay_identity,c(1.2,0.2),ylim=c(0,2),xlim=c(-.3,.1), tck=0, cex.main=1.4, main="", cex.lab=0.95, cex.axis=1, 
     pch=c(16,17), cex=2, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("grey30","grey60"))
abline(v=0, lty=2)
axis(1,at=seq(-.3,.1,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1"), tck=0.01, las=1)
axis(2, at=c(0.45,0.15,1.45,1.15),cex.axis=1.1,labels=c("Transgender","Gay/Lesbian","Transgender","Gay/Lesbian"), padj=0, las=1, tick=F)
legend("topleft", c("LGBTQ Respondents","Straight, Cisgender Respondents"), col=c("grey30","grey60"), pch=c(16,17), cex=0.95)
segments(gay_identity_ci_low_pooled, c(1.2,0.2), gay_identity_ci_high_pooled, c(1.2,0.2), lwd=1, col=c("grey30","grey60"))
segments(gay_identity_ci_low_pooled_90, c(1.2,0.2), gay_identity_ci_high_pooled_90, c(1.2,0.2), lwd=3, col=c("grey30","grey60"))

points(x=coef_from_baseline_trans_identity, y=c(1.5,0.5), pch=c(16,17), col=c("grey30","grey60"), cex=2)
segments(trans_identity_ci_low_pooled, c(1.5,0.5), trans_identity_ci_high_pooled, c(1.5,0.5), lwd=1, col=c("grey30","grey60"))
segments(trans_identity_ci_low_pooled_90, c(1.5,0.5), trans_identity_ci_high_pooled_90, c(1.5,0.5), lwd=3, col=c("grey30","grey60"))
dev.off()

#####################################################
# Figure E.9: Results by respondent Court knowledge #
#####################################################
# Subsetting to respondents who are high knowledge (4 correct answers)
dataset.high.knowledge.respondents <- support.dataset[which(support.dataset$knowledge4pt %in% c(4)),]

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs, high knowledge respondents
results.support.high.knowledge.respondents <- amce(support_nominee ~ judge_age + judge_race + judge_law_school + judge_job + judge_politics
                                                   + judge_transgender + judge_sexuality + judge_rhetoric, data=dataset.high.knowledge.respondents, 
                                                   baselines = baselines_list, cluster=T, respondent.id = "ResponseId")
summary(results.support.high.knowledge.respondents)

# Subsetting to respondents who are low knowledge (0/1/2/3 correct)
dataset.low.knowledge.respondents <- support.dataset[which(support.dataset$knowledge4pt %in% c(0:3)),]

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs, low knowledge respondents
results.support.low.knowledge.respondents <- amce(support_nominee ~ judge_age + judge_race + judge_law_school + judge_job + judge_politics
                                                  + judge_transgender + judge_sexuality + judge_rhetoric, data=dataset.low.knowledge.respondents, 
                                                  baselines = baselines_list, cluster=T, respondent.id = "ResponseId")
summary(results.support.low.knowledge.respondents)

### Plot of differences when moving from baseline ###
# Values come from above models, high knowledge respondents first then low knowledge respondents
coef_from_baseline_gay_knowledge <- c(-0.0529679,-0.0988787)
# Transgender, high knowledge respondents first then low knowledge respondents
coef_from_baseline_trans_knowledge <- c(-0.1174190,-0.1547815)

# Confidence intervals around the differences
gay_knowledge_se <- c(0.029718,0.025564)
trans_knowledge_se <- c(0.031549,0.026150)

gay_knowledge_ci_low_pooled <- coef_from_baseline_gay_knowledge - (gay_knowledge_se*1.96)
gay_knowledge_ci_high_pooled <- coef_from_baseline_gay_knowledge + (gay_knowledge_se*1.96)
gay_knowledge_ci_low_pooled_90 <- coef_from_baseline_gay_knowledge - (gay_knowledge_se*1.65)
gay_knowledge_ci_high_pooled_90 <- coef_from_baseline_gay_knowledge + (gay_knowledge_se*1.65)

trans_knowledge_ci_low_pooled <- coef_from_baseline_trans_knowledge - (trans_knowledge_se*1.96)
trans_knowledge_ci_high_pooled <- coef_from_baseline_trans_knowledge + (trans_knowledge_se*1.96)
trans_knowledge_ci_low_pooled_90 <- coef_from_baseline_trans_knowledge - (trans_knowledge_se*1.65)
trans_knowledge_ci_high_pooled_90 <- coef_from_baseline_trans_knowledge + (trans_knowledge_se*1.65)

# Plotting
pdf(file="figures/figure-e9-final.pdf", width=5.5,height=5.5)
par(mar=c(4.5,6.5,1,1), mgp=c(2,.25,0))
plot(coef_from_baseline_gay_knowledge,c(1.2,0.2),ylim=c(0,2),xlim=c(-.3,.1), tck=0, cex.main=1.4, main="", cex.lab=0.95, cex.axis=1, 
     pch=c(16,17), cex=2, xlab="Change in Support from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("grey30","grey60"))
abline(v=0, lty=2)
axis(1,at=seq(-.3,.1,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1"), tck=0.01, las=1)
axis(2, at=c(0.45,0.15,1.45,1.15),cex.axis=1.1,labels=c("Transgender","Gay/Lesbian","Transgender","Gay/Lesbian"), padj=0, las=1, tick=F)
legend("topleft", c("High Knowledge Respondents","Low Knowledge Respondents"), col=c("grey30","grey60"), pch=c(16,17), cex=0.95)
segments(gay_knowledge_ci_low_pooled, c(1.2,0.2), gay_knowledge_ci_high_pooled, c(1.2,0.2), lwd=1, col=c("grey30","grey60"))
segments(gay_knowledge_ci_low_pooled_90, c(1.2,0.2), gay_knowledge_ci_high_pooled_90, c(1.2,0.2), lwd=3, col=c("grey30","grey60"))

points(x=coef_from_baseline_trans_knowledge, y=c(1.5,0.5), pch=c(16,17), col=c("grey30","grey60"), cex=2)
segments(trans_knowledge_ci_low_pooled, c(1.5,0.5), trans_knowledge_ci_high_pooled, c(1.5,0.5), lwd=1, col=c("grey30","grey60"))
segments(trans_knowledge_ci_low_pooled_90, c(1.5,0.5), trans_knowledge_ci_high_pooled_90, c(1.5,0.5), lwd=3, col=c("grey30","grey60"))
dev.off()

##############################################################
# Figure E.10: Results for just cisgender, straight nominees #
##############################################################
# Subsetting to profiles that are just straight and cisgender nominees
dataset.straight.cisgender.support <- support.dataset[which(support.dataset$judge_transgender == "Not transgender" & support.dataset$judge_sexuality == "Straight"),]

# Revised gender variable to reflect more limited possible attribute options
dataset.straight.cisgender.support$judge_gender_alternative <-  as.factor(ifelse(dataset.straight.cisgender.support$judge_gender == "Man","Man","Woman"))

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs by party, just cisgender and straight nominee profiles
results.support.by.party.just.straight.cisgender.nominees <- amce(support_nominee ~ judge_age*Party + judge_race*Party 
                                                                  + judge_law_school*Party + judge_job*Party 
                                                                  + judge_politics*Party + judge_gender_alternative*Party 
                                                                  + judge_rhetoric*Party,
                                                                  data=dataset.straight.cisgender.support, respondent.varying="Party", baselines = baselines_list, cluster=T, 
                                                                  respondent.id = "ResponseId")
summary(results.support.by.party.just.straight.cisgender.nominees)

### Plot of differences when moving from baseline ###
# All respondents first, then D, then R
coefs_from_baseline_gender <- c(0.06865575,
                                -0.0105419,
                                0.0475174)

# Confidence intervals around the differences
gender_se <- c(0.039730,0.043026,0.057871)

gender_ci_low <- coefs_from_baseline_gender - (gender_se*1.96)
gender_ci_high <- coefs_from_baseline_gender + (gender_se*1.96)
gender_ci_low_90 <- coefs_from_baseline_gender - (gender_se*1.65)
gender_ci_high_90 <- coefs_from_baseline_gender + (gender_se*1.65)

# Plotting
pdf(file="figures/figure-e10-final.pdf", width=6.5,height=6.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(coefs_from_baseline_gender,c(1.2,1,0.8),ylim=c(0,2),xlim=c(-.2,.2), tck=0, cex.main=1.4, main="", cex.lab=1.2, cex.axis=1, 
     pch=c(16,17,15), cex=2.1, xlab="Change in Support from Baseline (Man)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
#lines(x=group, y=independents+jitter)
axis(1,at=seq(-.3,.2,by=.1), cex.axis=1.1, 
     labels=c("-.3","-.2","-.1","0",".1",".2"), tck=0.01, las=1)
axis(2, at=c(1),cex.axis=1.1,labels=c("Woman"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.4)
segments(gender_ci_low, c(1.2,1,0.8), gender_ci_high, c(1.2,1,0.8), lwd=1, col=c("black","blue","red"))
segments(gender_ci_low_90, c(1.2,1,0.8), gender_ci_high_90, c(1.2,1,0.8), lwd=3, col=c("black","blue","red"))
dev.off()

# Point estimate for all respondents
round(summary(results.support.by.party.just.straight.cisgender.nominees)$amce[3,3],3)
# p-value for all respondents
round(summary(results.support.by.party.just.straight.cisgender.nominees)$amce[3,6],3)

###################################
# Figure E.11: Legitimacy results #
###################################
# Subsetting to respondents who answered the legitimacy questions
legitimacy.dataset <- survey.dataset[which(!is.na(survey.dataset$legitimacy_additive)),]

# Rescaling legitimacy to range from 0-1
legitimacy.dataset$legitimacy_0_1 <- (legitimacy.dataset$legitimacy_additive)/5

# Setting baselines
baselines_list <- list()

# Estimate the AMCEs by party, legitimacy
results.legitimacy.by.party <- amce(legitimacy_0_1 ~ judge_age*Party + judge_race*Party 
                                    + judge_law_school*Party + judge_job*Party 
                                    + judge_politics*Party + judge_transgender*Party 
                                    + judge_sexuality*Party+ judge_rhetoric*Party,
                                    data=legitimacy.dataset, respondent.varying="Party", baselines = baselines_list, cluster=T, 
                                    respondent.id = "ResponseId")
summary(results.legitimacy.by.party)

### Appendix discussion of coefficients ###
# Gay penalty for Republicans, coefficient and p-value
round(summary(results.legitimacy.by.party)$Party3amce[14,3],3) # coefficient
round(summary(results.legitimacy.by.party)$Party3amce[14,6],3) # p-value
# Transgender penalty for Republicans, coefficient and p-value
round(summary(results.legitimacy.by.party)$Party3amce[13,3],3) # coefficient
round(summary(results.legitimacy.by.party)$Party3amce[13,6],3) # p-value

# Plot of differences when moving from baseline
# All respondents first, then D, then R
legit_coefs_from_baseline_sexuality <- c(-0.02346970,
                                         -0.00767770,
                                         -0.0322061)
# All respondents first, then D, then R
legit_coefs_from_baseline_gender <- c(-0.04821695,
                                      -0.01105730,
                                      -0.0879566)

# Confidence intervals around the differences
legit_sexuality_se <- c(0.012450,0.018039,0.018347)
legit_gender_se <- c(0.012373,0.017693,0.018639)

legit_sexuality_ci_low <- legit_coefs_from_baseline_sexuality - (legit_sexuality_se*1.96)
legit_sexuality_ci_high <- legit_coefs_from_baseline_sexuality + (legit_sexuality_se*1.96)
legit_sexuality_ci_low_90 <- legit_coefs_from_baseline_sexuality - (legit_sexuality_se*1.65)
legit_sexuality_ci_high_90 <- legit_coefs_from_baseline_sexuality + (legit_sexuality_se*1.65)

legit_gender_ci_low <- legit_coefs_from_baseline_gender - (legit_gender_se*1.96)
legit_gender_ci_high <- legit_coefs_from_baseline_gender + (legit_gender_se*1.96)
legit_gender_ci_low_90 <- legit_coefs_from_baseline_gender - (legit_gender_se*1.65)
legit_gender_ci_high_90 <- legit_coefs_from_baseline_gender + (legit_gender_se*1.65)

# Plotting
pdf(file="figures/figure-e11-final.pdf", width=6.5,height=6.5)
par(mar=c(4.5,5.75,1,1), mgp=c(2,.25,0))
plot(legit_coefs_from_baseline_sexuality,c(0.65,0.5,0.35),ylim=c(0,2),xlim=c(-.4,.2), tck=0, cex.main=1.4, main="", cex.lab=1.2, cex.axis=1, 
     pch=c(16,17,15), cex=2, xlab="Change in Legitimacy from Baseline (Cisgender or Straight)", xaxt="n", yaxt="n", ylab="", lwd=1.5, col=c("black","blue","red"))
abline(v=0, lty=2)
#lines(x=group, y=independents+jitter)
axis(1,at=seq(-.4,.2,by=.1), cex.axis=1.1, 
     labels=c("-.4","-.3","-.2","-.1","0",".1",".2"), tck=0.01, las=1)
axis(2, at=c(0.5,1.5),cex.axis=1.1,labels=c("Gay/Lesbian","Transgender"), padj=0, las=1, tick=F)
legend("topleft", c("All Respondents","Democrats","Republicans"), col=c("black","blue","red"), pch=c(16,17,15), cex=1.4)
segments(legit_sexuality_ci_low, c(0.65,0.5,0.35), legit_sexuality_ci_high, c(0.65,0.5,0.35), lwd=1, col=c("black","blue","red"))
segments(legit_sexuality_ci_low_90, c(0.65,0.5,0.35), legit_sexuality_ci_high_90, c(0.65,0.5,0.35), lwd=3, col=c("black","blue","red"))

points(x=legit_coefs_from_baseline_gender, y=c(1.65,1.5,1.35), pch=c(16,17,15), col=c("black","blue","red"), cex=2)
segments(legit_gender_ci_low, c(1.65,1.5,1.35), legit_gender_ci_high, c(1.65,1.5,1.35), lwd=1, col=c("black","blue","red"))
segments(legit_gender_ci_low_90, c(1.65,1.5,1.35), legit_gender_ci_high_90, c(1.65,1.5,1.35), lwd=3, col=c("black","blue","red"))
dev.off()
